package com.warren.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.warren.biz.entity.ProductCategory;
import com.warren.biz.mapper.ProductCategoryMapper;
import com.warren.biz.service.ProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.warren.util.RedisUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author warren
 * @since 2021-04-20
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    @Resource
    private RedisUtils redisUtils;

    @Override
    public List<ProductCategory> categoryTreeBuild(List<ProductCategory> productCategories) throws CloneNotSupportedException {
        if (productCategories.size() == 0){
            throw new Error("未查询到菜单信息");
        }
        //主菜单
        List<ProductCategory> menuList = new ArrayList<>();
        //子菜单
        List<ProductCategory> subMenuList = new ArrayList<>();
        //分类菜单的位置
        for (ProductCategory menu : productCategories){
            if (menu.getPid() == 0){
                menuList.add(menu);
            }else if(menu.getPid() != 0){
                subMenuList.add(menu);
            }
        }

        //子菜单组合到主菜单
        for (ProductCategory menu : subMenuList) {
            findSubMenu(productCategories, menu);
        }

        //排序
        sortSubMenu(menuList);

        return menuList;
    }

    @Override
    public List<ProductCategory> getAll() {
        QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
        List<ProductCategory> productCategoryAll = (List<ProductCategory>)redisUtils.get("ProductCategory_all");
        if (CollectionUtils.isEmpty(productCategoryAll)){
            productCategoryAll = this.baseMapper.selectList(queryWrapper.orderByAsc("pid"));
            redisUtils.set("ProductCategory_all",productCategoryAll);
        }
        return productCategoryAll;
    }

    @Override
    public void delMenu(Long id) {
        this.baseMapper.deleteById(id);
        redisUtils.del("ProductCategory_all");
    }

    @Override
    public void updateMenu(ProductCategory category) {
        this.baseMapper.updateById(category);
        redisUtils.del("ProductCategory_all");
    }

    @Override
    public void add(ProductCategory category) {
        this.baseMapper.insert(category);
        redisUtils.del("ProductCategory_all");
    }

    /**
     * 递归将子菜单组合到父菜单的children中
     * @param productCategories
     * @param item
     * @throws CloneNotSupportedException
     */
    private void findSubMenu(List<ProductCategory> productCategories,ProductCategory item) throws CloneNotSupportedException{
        for (ProductCategory productCategory : productCategories){
            if (item.getPid().equals(productCategory.getId())){
                productCategory.add(item);
                return;
            }
        }
    }

    private void sortSubMenu(List<ProductCategory> productCategories){
        productCategories.sort(ProductCategory.order());
        for (ProductCategory productCategory : productCategories){
            if (productCategory.getChildren().size() > 0){
                sortSubMenu(productCategory.getChildren());
            }
        }
    }
}
